// 去重
const arr = [1, 1, 2, 2]
const arr2 = [...new Set(arr)]
console.log(arr2)

// 判断元素是否在集合中
const set = new Set(arr)
const has = set.has(5)
console.log(has)

// 求交集
const set2 = new Set([2, 3])
const set3 = new Set([...set].filter(item => set2.has(item)))
console.log(set3)
// 求差集
const set4 = new Set([...set].filter(item => !set2.has(item)))
const set5 = new Set([...set2].filter(item => !set.has(item)))
console.log(set4)
console.log(set5)

// 使用set对象
let mySet = new Set()
mySet.add(1)
mySet.add(5)
mySet.add('asd')
const o = {
    a: 1,
    b: 2
}
mySet.add(o)
mySet.add({
    a: 1,
    b: 2
})
// 对象引用地址不同，所以能添加两个看似相同的对象
console.log(mySet)
const myHas = mySet.has(o)
console.log(myHas)
mySet.delete(5)
console.log(mySet)
// Set迭代
for (let item of mySet) console.log(item)
// 对于Set数据结构而言，keys和values的值是一样的
for (let item of mySet.keys()) console.log(item)
for (let item of mySet.values()) console.log(item)
for (let item of mySet.entries()) console.log(item)
// Set及Array互转
const myArr = [...mySet]
const myArr2 = Array.from(mySet)
const mySet2 = new Set([1, 2, 3, 4])